﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Html;
using System.Web.Routing;
using Ams.Mvc.Bootstrap.Annotations.MerchManagement.Web.Helpers;

namespace Ams.Mvc.Bootstrap
{
    public static class BreadcrumbExtensions
    {
        public static MvcHtmlString BreadcrumbLink<TModel>(this BootstrapHelper<TModel> helper, string linkText, string actionName, string controllerName)
        {
            return new MvcHtmlString(String.Format("<li>{0} <span class=\"divider\">/</span></li>", helper.Html.ActionLink(linkText, actionName, controllerName)));
        }
        public static MvcHtmlString BreadcrumbLink<TModel>(this BootstrapHelper<TModel> helper, string linkText, string actionName, string controllerName, RouteValueDictionary routeValues)
        {
            return new MvcHtmlString(String.Format("<li>{0} <span class=\"divider\">/</span></li>", helper.Html.ActionLink(linkText, actionName, controllerName, routeValues, null)));
        }
        public static MvcHtmlString BreadcrumbCurrentLocation<TModel>(this BootstrapHelper<TModel> helper, string text)
        {
            return new MvcHtmlString(String.Format("<li class=\"active\">{0}</li>", text));
        }

        public static HtmlString Breadcrumb<TModel>(this BootstrapHelper<TModel> helper)
        {
            var controller = helper.ViewContext.Controller;
            var action = helper.ViewContext.Controller.ValueProvider.GetValue("action").RawValue.ToString();

            var breadcrumbs = new List<HtmlString>();

            var currentBreadcrumb = BreadcrumbAttribute.GetBreadcrumbAttribute(controller, action);
            breadcrumbs.Add(BreadcrumbCurrentLocation(helper, currentBreadcrumb.Text));

            var routeValueMap = currentBreadcrumb.RouteValueMap;

            var i = currentBreadcrumb;
            while (true)
            {
                var parent = i.GetParentBreadcrumbAttribute(controller);
                if (parent == null) break;
                var parentController = String.IsNullOrWhiteSpace(i.ParentController) ? controller.ValueProvider.GetValue("controller").RawValue.ToString() : i.ParentController;
                var parentAction = String.IsNullOrWhiteSpace(i.ParentAction) ? "Index" : i.ParentAction;

                var routeValues = new RouteValueDictionary();
                routeValueMap.ForEach(m =>
                {
                    if (m.ParentAction != parentAction) return;
                    var value = helper.ViewContext.RouteData.Values[m.RouteParameter] ?? HttpContext.Current.Request[m.RouteParameter];
                    if (value == null) return;
                    routeValues.Add(m.ParentRouteParameter, value);
                });

                breadcrumbs.Insert(0, BreadcrumbLink(helper, parent.Text, parentAction, parentController, routeValues));
                i = parent;
            }

            var breadcrumbsText = String.Join(Environment.NewLine, breadcrumbs);
            const string ul = "<ul class=\"breadcrumb\">{0}{1}{0}</ul>";

            return new HtmlString(String.Format(ul, Environment.NewLine, breadcrumbsText));
        }
    }
}
